<template>
  <el-scrollbar :wrap-class="{wrapClass: true}">
    <div class="app-container">
      <div class="filter-container" size="small">
        <el-input
          v-model="listQuery.name"
          size="small"
          placeholder="oem名称"
          style="width: 200px;"
          class="filter-item"
          clearable
        />
        <el-select
          v-model="listQuery.state"
          size="small"
          placeholder="请选择状态"
          filterable
          clearable
          class="filter-item"
          style="width: 170px"
        >
          <el-option key="1" label="启用" value="1"/>
          <el-option key="2" label="禁用" value="2"/>
        </el-select>
        <el-button
          class="filter-item"
          size="small"
          icon="el-icon-search"
          type="primary"
          plain
          @click="search">
          {{ $t('table.search') }}
        </el-button>
        <el-button
          class="filter-item"
          size="small"
          icon="el-icon-plus"
          type="primary"
          @click="add"
        >
          {{ $t('table.add') }}
        </el-button>
        <el-button
          size="small"
          class="filter-item right"
          icon="el-icon-refresh"
          type="primary"
          plain
          @click="refresh"
        >
          刷新
        </el-button>
      </div>

      <el-table
        ref="multipleTable"
        v-loading="listLoading"
        :data="list"
        :height="listQuery.total>0 ? 'calc(100vh - 205px)' : 'calc(100vh - 170px)'"
        :header-cell-style="{background: '#F2F6FC'}"
        element-loading-text="Loading"
        fit
        border
        size="medium"
      >
        <el-table-column align="center" label="序号" width="50" type="index"/>
        <
        <el-table-column align="center" prop="id" label="OEM ID" width="80"/>
        <el-table-column align="center" prop="domain" label="域名" width="250">
          <template slot-scope="scope">
          <span><a style="color: rgb(64,158,255)" :href="'http://'+scope.row.domain"
                   target="_blank">{{ scope.row.domain }}</a></span>
          </template>
        </el-table-column>
        <el-table-column align="center" prop="name" label="OEM名称" width="200"/>

        <el-table-column label="左上角文字" min-width="200px" align="center" show-overflow-tooltip>
          <el-table-column label="左上角文字样式" min-width="200px" align="center" show-overflow-tooltip>
            <template slot-scope="scope">
              {{ scope.row.title }}
              <div class="divider">
                <el-divider/>
              </div>
              <strong v-if="scope.row.style == 1" style="color: #67C23A">单行显示</strong>
              <strong v-if="scope.row.style == 2" style="color: #409EFF">双行显示</strong>
            </template>
          </el-table-column>
        </el-table-column>
        <el-table-column label="状态" width="80" align="center">
          <template slot-scope="scope">
            <strong v-if="scope.row.state === 1" style="color: #67C23A">启用</strong>
            <strong v-else style="color: #F56C6C">禁用</strong>
          </template>
        </el-table-column>
        <el-table-column prop="address" label="创建时间" align="center" width="155px">
          <el-table-column prop="province" label="更新时间" align="center" width="155px">
            <template slot-scope="scope">
              <span>{{ scope.row.createdAt }}</span>
              <div class="divider">
                <el-divider/>
              </div>
              <span>{{ scope.row.updatedAt }}</span>
            </template>
          </el-table-column>
        </el-table-column>
        <el-table-column align="center" fixed="right" prop label="操作" width="300">
          <template slot-scope="scope">
            <el-button-group>
            <el-button
              type="primary"
              size="mini"
              icon="el-icon-edit"
              @click="edit(scope.row.id)"
            >编辑
            </el-button>
            <el-button
              type="primary"
              size="mini"
              @click="editEmailSettings(scope.row.id)"
            >邮箱配置
            </el-button>
            <el-button
              type="success"
              size="mini"
              icon="el-icon-setting"
              @click="allotUser(scope.row)"
            >
              分配OEM
            </el-button>
            <el-button
              :disabled="scope.row.id == 1"
              type="danger"
              size="mini"
              icon="el-icon-delete"
              @click="deleteRow(scope.row.id)"
            >删除
            </el-button>
            </el-button-group>
          </template>
        </el-table-column>
      </el-table>
      <!--分页-->
      <pagination
        v-show="listQuery.total>0"
        :total="listQuery.total"
        :page.sync="listQuery.page"
        :limit.sync="listQuery.limit"
        @pagination="fetchData"
      />

      <edit-page :id="id" :show.sync="showState" @closeAction="closeAction"/>
      <edit-email-settings :id="id" :show.sync="editEmailSettingsState" @closeAction="closeAction"/>
      <allot-page :show.sync="showAllotState" :oemId="id" :name="name"/>
    </div>
  </el-scrollbar>
</template>
<script>
import Pagination from '@/components/Pagination'
import EditPage from '@/views/systemManage/oem/edit'
import AllotPage from '@/views/systemManage/oem/allot'
import { getAction, postAction } from '@/api/common'
import EditEmailSettings from './editEmailSettings.vue'

export default {
  name: 'index',
  components: {
    Pagination,
    EditPage,
    AllotPage,
    EditEmailSettings
  },
  data() {
    return {
      listLoading: true,
      list: [],
      // 列表搜索条件
      listQuery: {
        page: 1,
        total: 0,
        limit: 20,
        name: '',
        state: ''
      },
      id: '',
      name: '',
      showState: false,
      showAllotState: false,
      editEmailSettingsState: false
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    async fetchData() {
      this.listLoading = true
      const { data } = await getAction(this.listQuery, 'oem/GetList')
      this.list = data.items
      this.listQuery.total = data.total
      this.listLoading = false
    },
    add() {
      this.id = ''
      this.showState = true
    },
    edit(id) {
      this.id = id
      this.showState = true
    },
    editEmailSettings(id) {
      this.id = id
      this.editEmailSettingsState = true
    },
    allotUser(row) {
      this.id = row.id
      this.name = row.name
      this.showAllotState = true
    },
    async deleteRow(id) {
      this.$confirm('是否确认删除', '提示', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await postAction({ id: id }, 'oem/delInfo')
        this.$notify({
          title: '成功',
          message: '操作成功',
          type: 'success',
          duration: 2000
        })
        this.refresh()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '取消操作'
        })
      })
    },
    search() {
      this.listQuery.page = 1
      this.fetchData()
    },
    refresh() {
      this.fetchData()
    },
    closeAction() {
      this.showState = false
      this.editEmailSettingsState = false
      this.fetchData()
    }
  }
}
</script>
<style scoped>
.divider /deep/ .el-divider--horizontal {
  margin: 10px 0;
}
</style>
